package cn.ehanghai.iot.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 阿里大鱼短信接口开发
 *
 * @Author: xumf
 * @Date: 2019/6/27 11:06
 */
public class SendMessage {

//    private static final String accessKeyId = "LTAIMFsl2SgvV2lA";
//    public static final String accessKeySecret = "XYFdLupTjwFCXL4t9mP7upL1BbQO06";
//    public static final String signName = "一间仿店";
//    public static final String templateCode = "SMS_126354787";

//      private static final String accessKeyId = "LTAI4FnGiCwY1zspJyvAy8QQ";
//      public static final String accessKeySecret = "7mdFwyqGWOnBg4lnh0IlTrfUMYw2Zi";
//      public static final String signName = "海上安监项目";
//      public static final String templateCode = "SMS_176928206";

    private static final String accessKeyId = "LTAI4Fk73Snw5n43FVarzTju";
    public static final String accessKeySecret = "ew91XHUrxs3H1qRjjinJUwToTAyulB";
    public static final String signName = "舟山市应急管理局";
    public static final String templateCode = "SMS_177544431";


    public static void sendSms(String phone, String json) throws ClientException {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化ascClient需要的几个参数
        final String product = "Dysmsapi";//短信API产品名称（短信产品名固定，无需修改）
        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名（接口地址固定，无需修改）
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
        request.setTemplateCode(templateCode);
//        String msgCode = InviteCodeGnerateUtil.getMsgCode();
//        request.setTemplateParam("{\"code\":\"" + msgCode + "\"}");
//        JSONObject object=new JSONObject();
//        object.put("code","23223");
        request.setTemplateParam(json);
        //请求失败这里会抛ClientException异常
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            //请求成功
            System.out.println("=====success====");
        } else {
            System.out.println("=====fail=======");
        }
    }

    public static void main(String[] args) {
        try {
            JSONObject object=new JSONObject();
            object.put("userName","管理员");
            object.put("elName","电子围栏预警");
            object.put("shipName","浙岱渔9999");
            sendSms("15158191391", object.toString());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
